# Clean up
rm(list = ls())
Sys.setenv(LANG = "en")

# Set working directory: please set your own
#setwd("~/Dropbox/sanctions_replication/")

# Load necessary packages
library(tidyverse)
library(readstata13)
library(lubridate)

# Import dataset
data <- read.dta13("data/survey_clean.dta")
 
# Make the plot
figure3 <- data %>%
  select("intent_phis_vox_num", "intent_verb_vox_num", "intent_insult_vox_num", 
         "intent_avoid_vox_num", "intent_deny_vox_num", "intent_gossip_vox_num", "no_intent_vox_num") %>%
  `colnames<-`(c("Vox_Physical", "Vox_Verbal", "Vox_Insult",
                 "Vox_Avoid", "Vox_Deny", "Vox_Gossip", "Vox_Nothing")) %>%
  # make into long format
  pivot_longer(cols = c(Vox_Physical:Vox_Nothing),
               names_to = "Group") %>%
  group_by(Group) %>%
  mutate(n = sum(!is.na(value))) %>%
  mutate(mean = mean(value, na.rm = T)) %>%
  mutate(sd = sd(value, na.rm = T)) %>%
  mutate(std_error = sd/sqrt(n)) %>%
  mutate(ci_upper = mean + 1.96 * std_error) %>%
  mutate(ci_lower = mean - 1.96 * std_error) %>%
  ungroup() %>%
  mutate(Outcome = ifelse(Group %in% c("Vox_Physical", "Vox_Physical_Others"), "Physical",
                          ifelse (Group %in% c("Vox_Verbal", "Vox_Verbal_Others"), "Verbal",
                                  ifelse(Group %in% c("Vox_Insult", "Vox_Insult_Others"), "Insult",
                                         ifelse(Group %in% c("Vox_Avoid", "Vox_Avoid_Others"), "Avoid\ninteraction",
                                                ifelse(Group %in% c("Vox_Deny", "Vox_Deny_Others"), "Deny\nhelp",
                                                       ifelse(Group %in% c("Vox_Gossip", "Vox_Gossip_Others"), "Gossip",
                                                              "Do\nnothing"))))
                          ))) %>%
  mutate(Nothing = ifelse(Group %in% c("Vox_Nothing", "Vox_Nothing_Others"), 1, 0)) %>%
  mutate(Outcome = fct_reorder(Outcome, Nothing)) %>%
  ggplot(aes(x = Outcome, y = value)) +
  geom_point(alpha = 0.02, position = position_jitter()) +
  scale_color_manual(values = c("blue", "black")) +
  theme_bw() +
  geom_errorbar(aes(x = Outcome, ymin = ci_lower, ymax = ci_upper, width = 0.1)) + 
  geom_point(aes(x = Outcome, y = mean)) +
  theme(legend.position = "none") +
  labs(x = "", y = "") +
  scale_y_continuous(breaks = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
figure3

ggsave("figures/figure3.png", plot = figure3, width = 10, height = 15, units = "cm")